package com.sbl.bajw.pc.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.DbManager;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.service.JWTTallGroupService;
import com.sbl.bajw.util.JsonUtil;

@Controller
@RequestMapping("/JWTTallGroup")
public class JWTTallGroupAction {
	
	@Resource
	private JWTTallGroupService service;
	
	@RequestMapping(value = "/getGroupList")
	public ModelAndView groupList(){
		ModelAndView mv = new ModelAndView("/view/manager/JWTGroup/List");
		return mv;
	}
	
	@RequestMapping(value = "/addGroupDialog")
	public ModelAndView addGroupDialog(){
		ModelAndView mv = new ModelAndView("/view/manager/JWTGroup/addGroupDialog");
		return mv;
	}
	
	@RequestMapping(value = "/addUserDialog")
	public ModelAndView addUserDialog(){
		ModelAndView mv = new ModelAndView("/view/manager/JWTGroup/userDialog");
		return mv;
	}
	
	@RequestMapping(value = "/getGroupListJson")
	public @ResponseBody String getGroupListJson(){
		List<HashMap<String, String>> models = service.getGroups();
		String result = JsonUtil.ConvertArrayToJson(models);
		return JsonResultUtil.Success(result);
	}
	
	@RequestMapping(value = "/getGroupUserList")
	public @ResponseBody String getGroupUserList(){
		String result = service.getGroupUser();
		return JsonResultUtil.Success(result);
	} 
	
	@RequestMapping(value = "/getJWTUser")
	public @ResponseBody String getJWTUser(){
		String result = service.getJWTUser();
		return JsonResultUtil.Success(result);
	} 	
	
	@RequestMapping(value = "/addGroup")
	public @ResponseBody String addGroup() throws Exception{
		DbManager.beginTransaction();
		service.insertGroup();
		DbManager.commit();
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/addUser")
	public @ResponseBody String addUser() throws Exception{
		DbManager.beginTransaction();
		String personidsStr = RequestUnit.getString("personid");
		String groupid = RequestUnit.getString("groupid");
		String equipmentcodesStr = RequestUnit.getString("equipmentcode");
		service.insertUserToTallGroup(personidsStr, groupid, equipmentcodesStr);
		DbManager.commit();
		return JsonResultUtil.Success();
	} 
	
	@RequestMapping(value = "/deleteGroup")
	public @ResponseBody String deleteGroup() throws Exception{
		DbManager.beginTransaction();
		service.deleteGroup();
		service.deleteUser();
		service.deleteUserCallparameter();
		DbManager.commit();
		return JsonResultUtil.Success();
	} 
	
	@RequestMapping(value = "/deleteUser")
	public @ResponseBody String deleteUser() throws Exception{
		DbManager.beginTransaction();
		service.deleteUser();
		service.deleteUserCallparameter();
		DbManager.commit();
		return JsonResultUtil.Success();
	} 
}
